import Cookies from 'js-cookie';

const app = {
    state: {
        sidebar: {
            opened: true
        },
        theme: 'default',
        cacheUpdated: 0, // 0 缓存、1 整页刷新、2 列表刷新
        livenewsChannels: Cookies.get('livenewsChannels') || '[]'
    },
    mutations: {
        TOGGLE_SIDEBAR: state => {
            if (state.sidebar.opened) {
                Cookies.set('sidebarStatus', 0);
            } else {
                Cookies.set('sidebarStatus', 1);
            }
            state.sidebar.opened = !state.sidebar.opened;
            
        },
        UPDATE_CACHE: (state, val) => {
            state.cacheUpdated = val;
        }
    },
    actions: {
        ToggleSideBar: ({ commit }) => {
            commit('TOGGLE_SIDEBAR')
        },
        UpdateCache: ({ commit }, val=1) => {
            // val 为空时，整页刷新
            // val 为true时，列表刷新
            if (typeof val === 'boolean' && val) {
                val = 2;
            }
            commit('UPDATE_CACHE', val);
        }
    }
};

export default app;
